package mypackage;

import mypackage.dao.ListDataProvider;
import mypackage.dao.ListDao;
import mypackage.dao.ListItemDao;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.KeywordFilterField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.container.MainScreen;

public class HomeScreen extends MainScreen {

	KeywordFilterField keywordFilterField;
	HelloBlackBerry helloBlackBerry;
	ListDataProvider dataProvider;

	public HomeScreen(HelloBlackBerry hb) {
		setTitle("Lists");
		dataProvider = new ListDataProvider();
		dataProvider.initFromVector(helloBlackBerry.dbObject.vector);
		helloBlackBerry = hb;
		keywordFilterField = new KeywordFilterField() {
			protected boolean navigationClick(int status, int time) {
				// TODO open the details screen here
				if (keywordFilterField.getSize() > 0)
					viewMenuItem.run();
				else
					addElementItem.run();
				return true;
			};

			protected boolean keyChar(char key, int status, int time) {
				if (key == Characters.BACKSPACE
						&& (keywordFilterField.getKeyword() == null || keywordFilterField
								.getKeyword().equals("")))
					deleteMenuItem.run();
				return super.keyChar(key, status, time);
			};
		};
		
		keywordFilterField.setSourceList(dataProvider, dataProvider);
		keywordFilterField.setEmptyString("* Add this one *", DrawStyle.HCENTER);
		viewMenuItem = new MenuItem("View", 0, 0) {
			public void run() {
				if (dataProvider.size() > 0)
					helloBlackBerry.openItemsScreen(keywordFilterField
							.getSelectedElement());
			};
		};
		deleteMenuItem = new MenuItem("Delete", 0, 0) {
			public void run() {
				if (Dialog.ask(Dialog.D_YES_NO, "Are you sure ?") == Dialog.YES) {
					if (dataProvider.size() > 0) {
						dataProvider.deleteList(keywordFilterField
								.getSelectedElement());
						keywordFilterField.updateList();
					}
				}
			};
		};
		addElementItem = new MenuItem("New", 0, 0) {
			public void run() {
				String keyword = keywordFilterField.getKeyword();
				keywordFilterField.setKeyword(null);
				if (keywordFilterField.getSize() > 0)
					keyword = null;
				String[] selections = { "Add", "Cancel" };
				final Dialog addDialog = new Dialog("New List", selections,
						null, 0, null);
				EditField inputField = new EditField("Name: ", keyword) {
					protected boolean keyChar(char key, int status, int time) {
						if (key == Characters.ENTER) {
							addDialog.select(0);
							return true;
						} else
							return super.keyChar(key, status, time);
					};
				};
				addDialog.add(inputField);

				if (addDialog.doModal() == 0) {
					dataProvider.addListWithName(inputField.getText());
					keywordFilterField.updateList();
				}
			}
		};
		editElementItem = new MenuItem("Edit", 0, 0) {
			public void run() {
				if (dataProvider.size() == 0)
					return;
				String str = ((ListDao)keywordFilterField.getSelectedElement()).listName;
				String[] selections = { "Done", "Cancel" };
				final Dialog addDialog = new Dialog("Edit list", selections,
						null, 0, null);
				EditField inputField = new EditField("Name: ", str) {
					protected boolean keyChar(char key, int status, int time) {
						if (key == Characters.ENTER) {
							addDialog.select(0);
							return true;
						} else
							return super.keyChar(key, status, time);
					};
				};
				addDialog.add(inputField);

				if (addDialog.doModal() == 0) {
					ListDao oldObject = (ListDao) keywordFilterField
							.getSelectedElement();
					oldObject.listName = inputField.getText();
					keywordFilterField.updateList();
				}
			}
		};
		
		add(keywordFilterField.getKeywordField());
		add(keywordFilterField);
		addMenuItem(addElementItem);
		addMenuItem(viewMenuItem);
		addMenuItem(editElementItem);
		addMenuItem(deleteMenuItem);
	}

	MenuItem viewMenuItem, deleteMenuItem, addElementItem, editElementItem;

	public boolean onClose() {
		helloBlackBerry.dbObject.vector = dataProvider.getVector();
		return super.onClose();
	}

//
//	ListFieldCallback listFieldCallback = new ListFieldCallback() {
//
//		public int indexOfList(ListField listField, String prefix, int start) {
//			// TODO Auto-generated method stub
//			return dataProvider.getVector().indexOf(prefix, start);
//		}
//
//		public int getPreferredWidth(ListField listField) {
//			// TODO Auto-generated method stub
//			return Display.getWidth();
//		}
//
//		public Object get(ListField listField, int index) {
//			// TODO Auto-generated method stub
//			return dataProvider.getVector().elementAt(index);
//		}
//
//		public void drawListRow(ListField listField, Graphics graphics,
//				int index, int y, int width) {
//			ListDao listDao = (ListDao) dataProvider.getVector().elementAt(index);
//			String str =  Characters.BALLOT_BOX + " " + listDao;
//			graphics.drawText(str, 10, y, 0, width);
//
//		}
//	};
//	
}
